<?php
/**
 * Copyright 2011 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '169668436505970',
  'secret' => 'd92a041380f2e3328efe75afbaefd503',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

// This call will always work since we are fetching public data.

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  
    <body>
    
    
       
    Danh sách bạn bè
    <?php 
//		$test = $facebook->api(array('method'=>'fql.query','query'=>"SELECT uid,name,first_name,middle_name,last_name,pic_square,hometown_location,current_location,profile_url,email,website FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"));
//		var_dump($test);die();
		
		$friends = $facebook->api('/me/friends');
//		var_dump($friends);die();
	?>
    
    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>

<?php $username = 'tuixachtranganh';
	$profile = $facebook->api('/'.$username);
?>
    <h3>Public profile of <?php echo $username;?></h3>
    <img src="https://graph.facebook.com/<?php echo $username;?>/picture">
    <?php echo $profile['name']; ?>
    
    
	Đã like 1 pages hay chưa
		<?php 
        
        if ($user) {
			try {
			$likes = $facebook->api("/me/likes/415537375177028");
			if( !empty($likes['data']) )
				echo "I like!";
			else
				echo "not a fan!";
			} catch (FacebookApiException $e) {
			error_log($e);
			$user = null;
			}
		}
	?>
 
  </body>
</html>
